using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using hao_Common.Response.Builders;
using hao_Common.Response.Models;

namespace hao_Common.Response.Extensions
{
    /// <summary>
    /// 控制器扩展方法
    /// </summary>
    public static class ControllerExtensions
    {
        #region 成功响应

        /// <summary>
        /// 返回成功结果（无数据）
        /// </summary>
        public static IActionResult SuccessResult(this ControllerBase controller, string message = "操作成功")
        {
            var response = ApiResponseBuilder.Success(message);
            SetRequestId(controller, response);
            return controller.Ok(response);
        }

        /// <summary>
        /// 返回成功结果（带数据）
        /// </summary>
        public static IActionResult SuccessResult<T>(this ControllerBase controller, T data, string message = "操作成功")
        {
            var response = ApiResponseBuilder.Success(data, message);
            SetRequestId(controller, response);
            return controller.Ok(response);
        }

        /// <summary>
        /// 返回分页成功结果
        /// </summary>
        public static IActionResult PagedResult<T>(this ControllerBase controller, IEnumerable<T> data, int pageNumber, int pageSize, int totalCount, string message = "查询成功")
        {
            var response = ApiResponseBuilder.PagedSuccess(data, pageNumber, pageSize, totalCount, message);
            SetRequestId(controller, response);
            return controller.Ok(response);
        }

        #endregion

        #region 失败响应

        /// <summary>
        /// 返回失败结果
        /// </summary>
        public static IActionResult FailResult(this ControllerBase controller, string message, string? errorCode = null, int statusCode = 400)
        {
            var response = ApiResponseBuilder.Fail(message, errorCode, statusCode);
            SetRequestId(controller, response);
            return controller.StatusCode(statusCode, response);
        }

        /// <summary>
        /// 返回验证失败结果
        /// </summary>
        public static IActionResult ValidationFailResult(this ControllerBase controller, string message = "参数验证失败")
        {
            var response = ApiResponseBuilder.ValidationFail(message);
            SetRequestId(controller, response);
            return controller.BadRequest(response);
        }

        /// <summary>
        /// 返回未授权结果
        /// </summary>
        public static IActionResult UnauthorizedResult(this ControllerBase controller, string message = "未授权访问")
        {
            var response = ApiResponseBuilder.Unauthorized(message);
            SetRequestId(controller, response);
            return controller.StatusCode(401, response);
        }

        /// <summary>
        /// 返回禁止访问结果
        /// </summary>
        public static IActionResult ForbiddenResult(this ControllerBase controller, string message = "禁止访问")
        {
            var response = ApiResponseBuilder.Forbidden(message);
            SetRequestId(controller, response);
            return controller.StatusCode(403, response);
        }

        /// <summary>
        /// 返回资源未找到结果
        /// </summary>
        public static IActionResult NotFoundResult(this ControllerBase controller, string message = "资源未找到")
        {
            var response = ApiResponseBuilder.NotFound(message);
            SetRequestId(controller, response);
            return controller.NotFound(response);
        }

        /// <summary>
        /// 返回服务器错误结果
        /// </summary>
        public static IActionResult ServerErrorResult(this ControllerBase controller, string message = "服务器内部错误")
        {
            var response = ApiResponseBuilder.ServerError(message);
            SetRequestId(controller, response);
            return controller.StatusCode(500, response);
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 设置请求ID
        /// </summary>
        private static void SetRequestId(ControllerBase controller, ApiResponse response)
        {
            var requestId = controller.HttpContext.TraceIdentifier;
            if (!string.IsNullOrEmpty(requestId))
            {
                response.RequestId = requestId;
            }
        }

        /// <summary>
        /// 设置请求ID（泛型版本）
        /// </summary>
        private static void SetRequestId<T>(ControllerBase controller, ApiResponse<T> response)
        {
            var requestId = controller.HttpContext.TraceIdentifier;
            if (!string.IsNullOrEmpty(requestId))
            {
                response.RequestId = requestId;
            }
        }

        #endregion
    }
}